###############################################################################
## AUTHOR: ALAN YAN
## DATE LAST UPDATED: 07/26/2020
## PURPOSE: ANALYZE YOUGOV CONJOINT DATA - LOG TRANSFORMING INCOME ATTRIBUTES
###############################################################################
rm(list = ls())

#### todo ####


#### PACKAGES ####
library(pacman)
p_load(tidyverse,
       estimatr,
       broom,
       haven,
       hrbrthemes,
       cjoint,
       formula.tools, 
       DeclareDesign,
       emmeans,
       lmtest,
       cregg,
       gridExtra,
       egg,
       car,
       stargazer)

#### LOAD DATA ####
df <- read_rds("01-yougov-conjoint/01-data/cleaned/yougov-conjoint-cleaned.rds")

#### CLEAN ####
df <- df %>%
  mutate(
    cje_income_log = case_when(
      cje_income == "$30,000 per year"  ~ 30000 ,
      cje_income == "$40,000 per year"  ~ 40000 , 
      cje_income == "$50,000 per year" ~ 50000 ,
      cje_income == "$60,000 per year"  ~ 60000 ,  
      cje_income == "$70,000 per year" ~ 70000 ,
      cje_income == "$80,000 per year"  ~ 80000 , 
      cje_income == "$90,000 per year" ~ 90000 ,
      cje_income == "$100,000 per year" ~ 100000,     
      cje_income == "$110,000 per year" ~ 110000, 
      cje_income == "$120,000 per year" ~ 120000,
      cje_income == "$130,000 per year" ~ 130000,
      cje_income == "$140,000 per year" ~ 140000,
      cje_income == "$150,000 per year" ~ 150000, 
      cje_income == "$160,000 per year" ~ 160000,     
      cje_income == "$170,000 per year" ~ 170000,
      cje_income == "$180,000 per year" ~ 180000,
      cje_income == "$190,000 per year" ~ 190000, 
      cje_income == "$200,000 per year" ~ 200000,    
      cje_income == "$210,000 per year" ~ 210000, 
      cje_income == "$220,000 per year" ~ 220000,
      cje_income == "$230,000 per year" ~ 230000,
      cje_income == "$240,000 per year" ~ 240000,
      cje_income == "$250,000 per year" ~ 250000, 
      cje_income == "$260,000 per year" ~ 260000,    
      cje_income == "$270,000 per year" ~ 270000,
      cje_income == "$280,000 per year" ~ 280000,
      cje_income == "$290,000 per year" ~ 290000, 
      cje_income == "$300,000 per year" ~ 300000) %>%
      log()
  )

#### TABLES ####
#Do people prefer firms with these features?
main_work_td <- lm(work_binary ~ 
                     as.factor(cje_corporate_gov) +
                     as.factor(cje_corporate_resp) +
                     as.factor(cje_firm_size) +
                     as.factor(cje_political_donations) + 
                     as.factor(cje_gender_ownership) + 
                     as.factor(cje_healthcare) + 
                     as.factor(cje_hours) + 
                     as.factor(cje_training) + 
                     as.factor(cje_location) + 
                     as.factor(cje_sick_leave) + 
                     as.factor(cje_parental_leave) + 
                     as.factor(cje_race_ownership) + 
                     as.factor(cje_retirement) + 
                     as.factor(cje_union) +
                     cje_income_log +
                     as.factor(cje_type_of_work) +
                     as.factor(cje_work_from_home) +
                     as.factor(cje_team_work) +
                     as.factor(cje_work_culture),
                   data = df)

#Do people believe some firms are better at dealing with complaints?
main_complaints_td <- lm(complaints_binary ~ 
                           as.factor(cje_corporate_gov) +
                           as.factor(cje_corporate_resp) +
                           as.factor(cje_firm_size) +
                           as.factor(cje_political_donations) + 
                           as.factor(cje_gender_ownership) + 
                           as.factor(cje_healthcare) + 
                           as.factor(cje_hours) + 
                           as.factor(cje_training) + 
                           as.factor(cje_location) + 
                           as.factor(cje_sick_leave) + 
                           as.factor(cje_parental_leave) + 
                           as.factor(cje_race_ownership) + 
                           as.factor(cje_retirement) + 
                           as.factor(cje_union) +
                           cje_income_log +
                           as.factor(cje_type_of_work) +
                           as.factor(cje_work_from_home) +
                           as.factor(cje_team_work) +
                           as.factor(cje_work_culture),
                         data = df
)

#Do people believe they'll have more responsibilties at some firms?
main_responsibility_td <- lm(responsibility_binary ~ 
                               as.factor(cje_corporate_gov) +
                               as.factor(cje_corporate_resp) +
                               as.factor(cje_firm_size) +
                               as.factor(cje_political_donations) + 
                               as.factor(cje_gender_ownership) + 
                               as.factor(cje_healthcare) + 
                               as.factor(cje_hours) + 
                               as.factor(cje_training) + 
                               as.factor(cje_location) + 
                               as.factor(cje_sick_leave) + 
                               as.factor(cje_parental_leave) + 
                               as.factor(cje_race_ownership) + 
                               as.factor(cje_retirement) + 
                               as.factor(cje_union) +
                               cje_income_log +
                               as.factor(cje_type_of_work) +
                               as.factor(cje_work_from_home) +
                               as.factor(cje_team_work) +
                               as.factor(cje_work_culture),
                             data = df)

#Do people believe they'll have more power at some firms?
main_power_td <- lm(power_binary ~ 
                      as.factor(cje_corporate_gov) +
                      as.factor(cje_corporate_resp) +
                      as.factor(cje_firm_size) +
                      as.factor(cje_political_donations) + 
                      as.factor(cje_gender_ownership) + 
                      as.factor(cje_healthcare) + 
                      as.factor(cje_hours) + 
                      as.factor(cje_training) + 
                      as.factor(cje_location) + 
                      as.factor(cje_sick_leave) + 
                      as.factor(cje_parental_leave) + 
                      as.factor(cje_race_ownership) + 
                      as.factor(cje_retirement) + 
                      as.factor(cje_union) +
                      cje_income_log +
                      as.factor(cje_type_of_work) +
                      as.factor(cje_work_from_home) +
                      as.factor(cje_team_work) +
                      as.factor(cje_work_culture),
                    data = df)

#### *MAKE TABLE (Table S11) ####
stargazer(main_work_td,
          main_power_td,
          main_responsibility_td,
          main_complaints_td,
          title = "Study 1: Results replicating main results with log transformed income conjoint attribute",
          covariate.labels = c("Privately owned by one individual",
                               "Publicly owned by shareholders",
                               "Workers are shareholder",
                               "Workers sit on the corporate board",
                               "Workers elect their managers",
                               "Primarily donates to Democrats",
                               "Primarily donates to Republicans",
                               "Union firm",
                               "Income (logged)"),
          dep.var.labels = c("Work",
                             "Power",
                             "Responsibilities",
                             "Complaints"),
          omit = c("cje_corporate_resp",
                   "cje_firm_size",
                   "cje_gender_ownership", 
                   "cje_healthcare", 
                   "cje_hours", 
                   "cje_training", 
                   "cje_location", 
                   "cje_sick_leave", 
                   "cje_parental_leave", 
                   "cje_race_ownership", 
                   "cje_retirement", 
                   "cje_type_of_work",
                   "cje_work_from_home",
                   "cje_team_work",
                   "cje_work_culture"),
          style = "apsr", 
          label = "tab:main-results-work-log-income",
          omit.stat = c("f", "ser"),
          se = list(starprep(main_work_td, se_type = "CR2", clusters = df$rid)[[1]],
                    starprep(main_power_td, se_type = "CR2", clusters = df$rid)[[1]],
                    starprep(main_responsibility_td, se_type = "CR2", clusters = df$rid)[[1]],
                    starprep(main_complaints_td, se_type = "CR2", clusters = df$rid)[[1]]),
          p = list(starprep(main_work_td, stat = "p.value", se_type = "CR2", clusters = df$rid)[[1]],
                   starprep(main_power_td, stat = "p.value", se_type = "CR2", clusters = df$rid)[[1]],
                   starprep(main_responsibility_td, stat = "p.value", se_type = "CR2", clusters = df$rid)[[1]],
                   starprep(main_complaints_td, stat = "p.value", se_type = "CR2", clusters = df$rid)[[1]])
)

